<?php
App::uses('AppController', 'Controller');

class BannerManageController extends AppController {
	var $helpers = array('Paginator','Html');
	public $user = "";
	var $components = array('Link','Strings','Files');
	public function beforeFilter() {
		parent::beforeFilter();
		$this->user = parent::checkAdminOrManagerLogin();
	}
	/**
	* hiển thị 
	*/
	public function admin_index() {
		$this -> loadModel('Banner');
		/*$this->paginate = array(
                                'limit' => ITEM_PER_PAGE,
                                'order' => array('id' => 'desc'),
                             );
		$conditions = array("Banner.deleted" => 0);
        $data = $this->paginate("Banner",$conditions);
        $this->set("data",$data);*/

		$this->Banner->recursive = 0;
		$this->paginate = array('Banner'=>array('limit'=>ITEM_PER_PAGE));
		$this->set('data', $this->paginate('Banner'));
		
		$this->set("title_for_layout","Quảng cáo");
	}
	
	public function admin_add(){
		
		$this -> loadModel('Category');
		$this -> loadModel('Banner');
		
		$cat = $this->Category->findAllData();
		$this->set("cat",$cat);
		/*echo "<pre>";
		print_r($cat);
		exit;*/
		if ($this -> request -> is('post')) {
			$new = $this->request->data;
			$new["img"] = $this->Files->uploadFile(UPLOAD_IMG_BANNER, $this->data['img']['tmp_name'], ROOT_IMG_BANNER, $this->data['img']['name']);
			$new["deleted"] = 0;
			$new['created_by'] = $this->user['id'];
			$new['created_at'] = date("Y-m-d H:i:s");
			$new['updated_by'] = $this->user['id'];
			$new['updated_at'] = date("Y-m-d H:i:s");
			$this->Banner->save($new);
			$this->Session->setFlash('Thêm banner thành công');
			$this->redirect(array('action' => 'index'));
		}
		$this->set("title_for_layout","Thêm banner");
	}
	
	public function admin_edit($id){
		$this -> loadModel('Category');
		$this -> loadModel('Banner');
		
		$cat = $this->Category->findAllData();
		$this->set("cat",$cat);
		
		$conditions = array("Banner.id" => $id);
		
		$data = $this->Banner->find('first', array('conditions' => $conditions));
		if(empty($data)){
			$this->redirect(array('action' => 'index'));
		}
		$this->set("data",$data);
		if ($this -> request -> is('post')) {
			$new = $this->request->data;
			if(!empty($this->data['img']['name'])){
				$new["img"] = $this->Files->uploadFile(UPLOAD_IMG_BANNER, $this->data['img']['tmp_name'], ROOT_IMG_BANNER, $this->data['img']['name']);
			}else{
				unset($new["img"]);
			}
			$new['id'] = $id;
			$new['updated_by'] = $this->user['id'];
			$new['updated_at'] = date("Y-m-d H:i:s");
			if($this->Banner->save($new)){
				
				if(!empty($new["img"])){
					$path = WWW_ROOT.substr($new['imagOld'],1);
					$this->Files -> deleteFile($path);
				}
				
				$this->Session->setFlash('Chỉnh sửa danh mục thành công');
				$this->redirect(array('action' => 'index'));
			}
			
		}
		
		$this->set("title_for_layout","Sửa Banner");
	}
	
	public function admin_delete($id){
		$this -> loadModel('Banner');
		$conditions = array("Banner.id" => $id);
		$data = $this->Banner->find('first', array('conditions' => $conditions));
		if(!empty($data)){
			$update['deleted'] = 1;
			$update['id'] = $id;
			$update['updated_by'] = $this->user['id'];
			$update['updated_at'] = date("Y-m-d H:i:s");
			$this->Banner->save($update);
		}
		$this->redirect(array('action' => 'index'));
	}

}
?>